﻿// BasicTypeTransform.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <memory>

class Object {
public:
    Object() {
        std::cout << "Object constructor called!" << std::endl;
    }
    ~Object() {
        std::cout << "Object destructor called!" << std::endl;
    }
    void show() {
        std::cout << "Object show called!" << std::endl;
    }
};

void test() {
    std::cout << "Start test!\n";

    std::shared_ptr<Object> obj = std::make_shared<Object>();
    // 输出引用计数
    std::cout << obj.use_count() << std::endl;

    std::shared_ptr<Object> obj2 = obj;
    std::cout << obj.use_count() << " " << obj2.use_count() << std::endl;

    std::shared_ptr<Object> obj3 = obj;
    std::cout << obj.use_count() << " " << obj2.use_count() << " " << obj3.use_count() << std::endl;

    Object *p = obj.get();

    obj.reset();
    obj2.reset();
    obj3.reset();

    std::cout << obj.use_count() << " " << obj2.use_count() << " " << obj3.use_count() << std::endl;
}

void test2() {
    std::cout << "Start test2!\n";
    Object *obj = new Object;

    std::shared_ptr<Object> sp1 = std::shared_ptr<Object>(obj);

    // 输出引用计数
    std::cout << sp1.use_count() << std::endl;

    std::shared_ptr<Object> sp2 = sp1;
    std::cout << sp1.use_count() << " " << sp2.use_count() << std::endl;

    std::shared_ptr<Object> sp3 = sp1;
    std::cout << sp1.use_count() << " " << sp2.use_count() << " " << sp3.use_count() << std::endl;

    // sp1.reset();
    // sp2.reset();
    // sp3.reset();

    delete obj;

    std::cout << sp1.use_count() << " " << sp2.use_count() << " " << sp3.use_count() << std::endl;

    if (obj) {
        std::cout << "object has not been release!" << std::endl;
        obj->show();
    } else {
        std::cout << "object has been release!" << std::endl;
        obj->show();
    }
    // 注意，这里由于 shared_ptr 指向的资源已经被释放，所以 test2执行结束时，会调用 shared_ptr 的析构函数，而此时资源已被delete，会崩溃
}

void test3() {
    std::cout << "Start test3!\n";

    Object *obj = new Object;

    std::shared_ptr<Object> sp1 = std::shared_ptr<Object>(obj);

    // 输出引用计数
    std::cout << sp1.use_count() << std::endl;

    std::shared_ptr<Object> sp2 = sp1;
    std::cout << sp1.use_count() << " " << sp2.use_count() << std::endl;

    std::shared_ptr<Object> sp3 = sp1;
    std::cout << sp1.use_count() << " " << sp2.use_count() << " " << sp3.use_count() << std::endl;

    sp1 = nullptr;
    sp2 = nullptr;
    sp3 = nullptr;

    // 这里会崩溃 因为资源已释放
    delete obj;

    std::cout << sp1.use_count() << " " << sp2.use_count() << " " << sp3.use_count() << std::endl;

    if (sp1) {
        std::cout << "object has not been release!" << std::endl;
        sp1->show();
    } else {
        std::cout << "object has been release!" << std::endl;
        sp1->show();
    }

    if (obj) {
        std::cout << "object has not been release!" << std::endl;
        obj->show();
    } else {
        std::cout << "object has been release!" << std::endl;
        obj->show();
    }
}

int main()
{
    std::cout << "Start Process!\n";

    test();

    // test2();

    test3();

    return 0;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
